const chalk = require('chalk')
const express = require('express')
const app = express()

function Routers (routes, logout, middle=[]) {
    routes.forEach(item => {
        const middleware = []
        if (logout)
            console.log(chalk.yellow(item.description))
        if (item.children) {
            item.use ? item.use instanceof Array ? middleware.push(...item.use) : middleware.push(item.use) : null
            Routers(item.children, logout, middleware)
        } else {
            if (!item.method) {
                throw new Error(item.path + '接口' + '没有指定请求方法method')
            }
            if (!item.use) {
                throw new Error(item.path + '接口' + '没有指定控制层use')
            }
            item.use instanceof Array ? app[item.method.toLowerCase()].call(app, item.path, ...middle, ...item.use) : app[item.method].call(app, item.path, ...middle, item.use)
        }
    })
}

module.exports = {
    app,
    Routers,
}
